<?php
namespace app\common\tools;

use app\common\tools\Req;
use app\common\exception\ThrowException;
/**
* 异常类工厂调用基类
*/
class ReqHttp
{
    const NOT_METHOD_MSG = 3000001;
    /**
     * 默认header
     */
    const HEADER = [
        //"Content-type" => "application/json",
        "Content-type" => "application/x-www-form-urlencoded; charset=UTF-8",
        "Accept-Encoding" => "gzip, deflate, br",
        "Accept" => "*/*",
        "User-Agent" => "PostmanRuntime/7.26.3",
        "Connection" => "keep-alive",
        "Content-Length" => "",
    ];

    public static function get(String $url, Array $params = [], Array $header = [])
    {
        $header = self::header($header);
        return Req::init(self::host())->get($url, $params, $header);
    }

    public static function post(String $url, Array $params = [], Array $header = [])
    {
        $header = self::header($header);
        return Req::init(self::host())->post($url, $params, $header);
    }

    public static function delete(String $url, Array $params = [], Array $header = [])
    {
        $header = self::header($header);
        return Req::init(self::host())->delete($url, $params, $header);
    }

    public static function put(String $url, Array $params = [], Array $header = [])
    {
        $header = self::header($header);
        return Req::init(self::host())->put($url, $params, $header);
    }

    public static function patch(String $url, Array $params = [], Array $header = [])
    {
        $header = self::header($header);
        return Req::init(self::host())->patch($url, $params, $header);
    }

    public static function run(String $method, String $url, Array $params = [], Array $header = [])
    {
        if (!in_array($method, Req::METHOD_LISTS)) {
            ThrowException::res(self::NOT_METHOD_MSG);
        }

        return self::$method($url, $params, $header);
    }

    /**
     * header封装
     */
    protected static function header(Array $header = [])
    {
        foreach (self::HEADER as $key => $value) {
            if (!key_exists($key, $header)) {
                $header[$key] = $value;
            }
        }

        return $header;
    }

    /**
     * 获取域名
     */
    protected static function host()
    {
        return env('app.host', 'https://11000000715.vyohui.cn');
    }
}